import 'package:card_swiper/card_swiper.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:yqys_target/extensions/hex_color.dart';
import 'package:yqys_target/gen/assets.gen.dart';
import 'package:yqys_target/pages/launch_charity/launch_charity_page.dart';
import 'package:yqys_target/pages/my_charity/my_charity_page.dart';
import 'package:yqys_target/pages/my_credential/my_credential_page.dart';
import 'package:yqys_target/pages/upload_credential/upload_credential_page.dart';

import '../utils/yq_colors.dart';
import '../widgets/yq_list_card.dart';
import 'more_welfare_page.dart';
import 'organization/views/organization_auth_page.dart';
import 'raise_detail_page.dart';

//首页分类入口item
class HomeCateItemsBar extends StatelessWidget {
  final Function(int) onTap;
  final List<Map<String, dynamic>> functionList;

  const HomeCateItemsBar({
    super.key,
    required this.functionList,
    required this.onTap,
  });

  @override
  Widget build(BuildContext context) {
    return InkWell(
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: List.generate(
          functionList.length,
          (index) {
            final item = functionList[index];
            return InkWell(
              onTap: () {
                onTap(index);
              },
              child: SizedBox(
                width: 64,
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    SizedBox(
                      width: 48,
                      height: 48,
                      child: Image.asset(
                        item['icon'],
                        fit: BoxFit.cover,
                      ),
                    ),
                    const SizedBox(height: 8),
                    Text(
                      item['title'],
                      style: const TextStyle(
                        fontSize: 15,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ],
                ),
              ),
            );
          },
        ),
      ),
    );
  }
}


class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  // Banner图片列表
  final List<String> bannerList = [
    Assets.images.banner1.path,
    Assets.images.banner2.path,
  ];

  // 功能入口图标数据
  final List<Map<String, dynamic>> cateItemList = [
    {
      'icon': Assets.icons.homeCateItemMore.path,
      'title': '更多益事',
    },
    {
      'icon': Assets.icons.homeCateItemJigou.path,
      'title': '机构入驻',
    },
    {
      'icon': Assets.icons.homeCateItemMsg.path,
      'title': '消息通知',
    },
    {
      'icon': Assets.icons.homeCateItemComingsoon.path,
      'title': '敬请期待',
    },
  ];

  @override
  Widget build(BuildContext context) {
    return AnnotatedRegion(
      value: const SystemUiOverlayStyle(
        statusBarColor: Colors.transparent,
        statusBarIconBrightness: Brightness.dark,
        statusBarBrightness: Brightness.dark,
      ),
      child: DefaultTextStyle(
        style: TextStyle(
          color: YQColors.primary,
        ),
        child: Scaffold(
          body: CustomScrollView(
            slivers: [
              SliverToBoxAdapter(
                child: AspectRatio(
                  aspectRatio: 374.0 / 284.0,
                  child: Container(
                    decoration: BoxDecoration(
                      borderRadius:const BorderRadius.only(
                        bottomLeft: Radius.circular(14),
                        bottomRight: Radius.circular(14),
                      ),
                      image: DecorationImage(
                        image: Assets.images.homeNaviBg.provider(),
                        fit: BoxFit.cover,
                      ),
                    ),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        const SizedBox(height: 50),
                        Padding(
                          padding: const EdgeInsets.only(left: 16),
                          child: ShaderMask(
                            shaderCallback: (bounds) => LinearGradient(
                              colors: [
                                HexColor.fromHexWithoutAlpha("#96FAB7"), // 浅绿色
                                HexColor.fromHexWithoutAlpha("#08C164"), // 深绿色
                              ],
                              begin: Alignment.topCenter,
                              end: Alignment.bottomCenter,
                            ).createShader(bounds),
                            child: InkWell(
                              onTap: () {
                                Navigator.push(
                                  context,
                                  MaterialPageRoute(
                                    builder: (context) => const MyCharityPage(),
                                  ),
                                );
                              },
                              child: const Text(
                                '益事赋能',
                                style: TextStyle(
                                  fontSize: 30,
                                  color: Colors.white,
                                  fontWeight: FontWeight.bold,
                                ),
                              ),
                            ),
                          ),
                        ),
                        const SizedBox(height: 8),
                        InkWell(
                          onTap: () {
                            Navigator.push(
                              context,
                              MaterialPageRoute(builder: (context) => const MyCredentialPage()),
                            );
                          },  
                          child: const Padding(
                            padding:  EdgeInsets.only(left: 16),
                            child:  Text(
                              '将每一份爱心化作改变世界的力量',
                              style: TextStyle(
                                fontSize: 16,
                                color: Colors.white,
                              ),
                            ),
                          ),
                        ),
                        const SizedBox(height: 16),
                        Expanded(
                          child: Padding(
                            padding: const EdgeInsets.symmetric(horizontal: 16),
                            child: ConstrainedBox(
                              constraints: const BoxConstraints(maxHeight: 140),
                              child: Swiper(
                                itemBuilder: (context, index) {
                                  return Image.asset(
                                    bannerList[index],
                                    fit: BoxFit.cover,
                                  );
                                },
                                itemCount: bannerList.length,
                                pagination: const SwiperPagination(),
                                autoplay: true,
                              ),
                            ),
                          ),
                        ),
                        const SizedBox(height: 16),
                      ],
                    ),
                  ),
                ),
              ),

              // 功能入口部分
              SliverPadding(
                padding:
                    const EdgeInsets.symmetric(horizontal: 16, vertical: 16),
                sliver: SliverToBoxAdapter(
                  child: HomeCateItemsBar(
                    functionList: cateItemList,
                    onTap: (index) {
                      // 处理点击事件
                      if (index == 0) {
                        Navigator.push(
                          context,
                          MaterialPageRoute(
                            builder: (context) => const MoreWelfarePage(),
                          ),
                        );
                      } else if (index == 1) {
                        Navigator.push(
                          context,
                          MaterialPageRoute(
                            builder: (context) => const OrganizationAuthPage(),
                          ),
                        );
                      } else if (index == 2) {
                        Navigator.push(
                          context,
                          MaterialPageRoute(
                            builder: (context) => const UploadCredentialPage(),
                          ),
                        );
                      } else if (index == 3) {
                        Navigator.push(
                          context,
                          MaterialPageRoute(
                            builder: (context) => const LaunchCharityPage(),
                          ),
                        );
                      }
                    },
                  ),
                ),
              ),

              // 公益项目标题
              const SliverPadding(
                padding: EdgeInsets.only(
                  left: 16,
                  right: 16,
                ),
                sliver: SliverToBoxAdapter(
                  child: HomePublicWelfareListTitleBar(),
                ),
              ),

              // 公益项目列表
              SliverPadding(
                padding: const EdgeInsets.all(16),
                sliver: SliverList(
                  delegate: SliverChildBuilderDelegate(
                    (context, index) {
                      return PublicWelfareCard(
                        imageUrl: Assets.images.project1.path,
                        title:
                            index % 2 == 0 ? '乡村留守儿童免费午餐' : '捐爱心蛋，助力乡村老人好好吃饭',
                        progress: index % 2 == 0 ? 0.869 : 0.552,
                        progressText: index % 2 == 0 ? '86.9%' : '55.2%',
                        target: '1亿',
                        onTap: () {
                          Navigator.push(
                            context,
                            MaterialPageRoute(
                              builder: (context) => RaiseDetailPage(
                                progress: index % 2 == 0 ? 0.869 : 0.552,
                                progressText: index % 2 == 0 ? '86.9%' : '55.2%',
                                target: '1亿',
                                title: index % 2 == 0 
                                    ? '乡村留守儿童免费午餐'
                                    : '捐爱心蛋，助力乡村老人好好吃饭',
                                description: '让清江河大龙潭至旗峰坝路段所有的小孩子都可以自由的欢笑我们还可以更美好',
                              ),
                            ),
                          );
                        },
                      );
                    },
                    childCount: 10,
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}


class HomePublicWelfareListTitleBar extends StatelessWidget {
  const HomePublicWelfareListTitleBar({
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Row(
          children: [
            Image.asset(
              Assets.icons.homeListTitleIcon.path,
              width: 20,
              height: 17,
            ),
            const SizedBox(width: 8),
            const Text(
              '公益项目',
              style: TextStyle(
                fontSize: 18,
                fontWeight: FontWeight.bold,
              ),
            ),
          ],
        ),
        Row(
          children: [
            Text(
              '更多',
              style: TextStyle(color: YQColors.primary),
            ),
            Icon(
              Icons.arrow_forward_ios,
              color: YQColors.primary,
              size: 10,
            ),
          ],
        ),
      ],
    );
  }
}